package com.foreknow.view;

import com.foreknow.model.Business;
import com.foreknow.service.impl.BusinessServiceImpl;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JButton;

public class Add extends JFrame {

	private JPanel contentPane;
	private JTextField password;
	private JTextField businessName;
	private JTextField businessAddress;
	private JTextField businessExplain;
	private JTextField starPrice;
	private JTextField deliveryPrice;
	private BusinessServiceImpl businessServiceImpl = new BusinessServiceImpl();
	private List<Business> list;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Add frame = new Add();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Add() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		password = new JTextField();
		password.setBounds(125, 67, 238, 21);
		contentPane.add(password);
		password.setColumns(10);
		
		businessName = new JTextField();
		businessName.setBounds(125, 36, 238, 21);
		contentPane.add(businessName);
		businessName.setColumns(10);
		
		businessAddress = new JTextField();
		businessAddress.setBounds(125, 98, 238, 21);
		contentPane.add(businessAddress);
		businessAddress.setColumns(10);
		
		businessExplain = new JTextField();
		businessExplain.setBounds(125, 129, 238, 21);
		contentPane.add(businessExplain);
		businessExplain.setColumns(10);
		
		starPrice = new JTextField();
		starPrice.setBounds(125, 160, 238, 21);
		contentPane.add(starPrice);
		starPrice.setColumns(10);
		
		deliveryPrice = new JTextField();
		deliveryPrice.setBounds(125, 191, 238, 21);
		contentPane.add(deliveryPrice);
		deliveryPrice.setColumns(10);
		
		JLabel name = new JLabel("\u5546\u5BB6\u540D\u79F0");
		name.setBounds(32, 39, 58, 15);
		contentPane.add(name);
		
		JButton Commit = new JButton("\u63D0\u4EA4");
		Commit.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				businessServiceImpl = new BusinessServiceImpl();
				Business business = new Business();
				String name = businessName.getText();
				String pass = password.getText();
				String address = businessAddress.getText();
				String explain = businessExplain.getText();
				Double starprice = Double.valueOf(starPrice.getText());
				Double delv = Double.valueOf(deliveryPrice.getText());
				business.setBusinessName(name);
				business.setBusinessPassword(pass);
				business.setBusinessAddress(address);
				business.setBusinessExplain(explain);
				business.setStarPrice(starprice);
				business.setDeliveryPrice(delv);
				if (businessServiceImpl.save(business)){
					System.out.println("添加成功");
					MainWindow mainWindow = new MainWindow();
					BusinessTableModel businessTable = new BusinessTableModel();
					businessTable.setData(list);
					Add.this.dispose();
				}else {
					System.out.println("添加失败");
				}
			}
		});
		Commit.setBounds(87, 230, 97, 23);
		contentPane.add(Commit);
		
		JButton Clean = new JButton("\u53D6\u6D88");
		Clean.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				businessName.setText("");
				password.setText("");
				businessAddress.setText("");
				businessExplain.setText("");
				starPrice.setText("");
				deliveryPrice.setText("");
			}
		});
		Clean.setBounds(266, 230, 97, 23);
		contentPane.add(Clean);
		
		JLabel pass = new JLabel("\u5BC6\u7801");
		pass.setBounds(32, 70, 58, 15);
		contentPane.add(pass);
		
		JLabel address = new JLabel("\u5546\u5BB6\u5730\u5740");
		address.setBounds(32, 101, 58, 15);
		contentPane.add(address);
		
		JLabel explain = new JLabel("\u5546\u5BB6\u4ECB\u7ECD");
		explain.setBounds(32, 132, 58, 15);
		contentPane.add(explain);
		
		JLabel price = new JLabel("\u8D77\u9001\u8D39");
		price.setBounds(32, 163, 58, 15);
		contentPane.add(price);
		
		JLabel delv = new JLabel("\u914D\u9001\u8D39");
		delv.setBounds(32, 194, 58, 15);
		contentPane.add(delv);

		this.setVisible(true);
	}
}
